<template>
  <div class="app-container">
    <!-- 面试技巧 -->
    <div class="top">
      <el-form ref="form" :model="form"  :inline="true" label-width="80px">
        <el-form-item label="关键字">
          <el-input v-model="form.keyword" placeholder="根据文章标题搜索"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="form.state" placeholder="请选择">
            <el-option
              v-for="item in stateOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button size="medium" @click="clear">清除</el-button>
          <el-button type="primary"  size="medium" @click="search">搜索</el-button>
        </el-form-item>
      </el-form>
      <el-button type="success" icon="el-icon-edit" @click="addArt">新增技巧</el-button>
    </div>
    <!-- 提示 -->
    <el-alert
      :title="`数据一共${total}条`"
      type="info"
      :closable="false"
      show-icon>
    </el-alert>
    <!-- 表格 -->
    <el-table :data="tableData" style="width: 100%">
      <el-table-column  type="index" label="序号" width="80"></el-table-column>
      <el-table-column  prop="title" label="文章标题"></el-table-column>
      <el-table-column  prop="visits" label="阅读数" width="100"></el-table-column>
      <el-table-column  prop="username" label="录入人" ></el-table-column>
      <el-table-column  prop="createTime" label="录入时间" >
        <template #default="{row}">
          {{getTime(row.createTime)}}
        </template>
      </el-table-column>
      <el-table-column  prop="state" label="状态" width="180">
      <template #default="{row}">
          {{`${row.state? '已开启': '已禁用'}`}}
        </template>
      </el-table-column>
      <el-table-column  label="操作">
        <template #default="{row}">
          <el-button type="text">预览</el-button>
          <el-button type="text" @click="changState(row.id, row.state)">
            {{row.state? '启用' : '禁用'}}
          </el-button>
          <el-button type="text" :disabled="row.state===1" @click="edit(row.id)">修改</el-button>
          <el-button type="text" :disabled="row.state===1" @click="del(row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <div class="bottom">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="form.page"
        :page-sizes="[3, 5, 10]"
        :page-size="form.pagesize"
        layout="prev, pager, next, sizes, jumper"
        :total="total">
      </el-pagination>
    </div>
    <el-dialog
      :title="title"
      :visible="dialogVisible"
      width="50%"
      @close="dialogClose">
      <el-form :model="addForm" :rules="rules" ref="addForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="文章标题" prop="title">
          <el-input v-model="addForm.title" placeholder="请输入文章标签"></el-input>
        </el-form-item>
        <el-form-item label="文章内容" prop="articleBody">
        <!-- 富文本 -->
        <quill-editor v-model="addForm.articleBody"></quill-editor>
        </el-form-item>
        <el-form-item label="视频地址">
          <el-input v-model="addForm.videoURL"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogClose">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>  
import {articlesList, articlesAdd, articlesDetail, articlesUpdate, articlesDel, articlesState} from '@/api/articles'
import {parseTime} from '@/utils/index'
export default {
  name: 'Articles',
  data() {
    return {
      form: {
        page: 1,
        pagesize: 10,
      },
      // 新增表单
      addForm: {},
      stateOptions: [
        {label: '启用', value: 1},
        {label: '禁用', value: 0}
      ],
      total: 0,
      tableData: [],
      dialogVisible: false,
      title:'',
      rules: {
        title: [
          { required: true, message: '请输入文章标题', trigger: 'blur' },
        ],
        articleBody: [
          { required: true, message: '请输入文章内容', trigger: 'blur' },
        ],
        // videoURL: [
        //   { required: true, message: '请输入视频地址', trigger: 'blur' },
        // ],
      }
    }
  },
  mounted() {
    this.getTableData()
  },
  methods: {
    // 清除
    clear() {
      
    },
    // 搜索
    search() {

    },
    async getTableData() {
      console.log(this.form);
      const res = await articlesList(this.form)
      console.log(res);
      this.total = res.counts
      this.tableData = res.items
    },
    getTime(time) {
      return parseTime(time)
    },
    // 新增技巧
    addArt() {
      this.title= "新增技巧"
      this.dialogVisible = true
    },
    // 关闭弹窗
    dialogClose() {
      this.dialogVisible = false
    },
    // 确认按钮
    confirm(){
      // 兜底校验
        this.$refs.addForm.validate(async(valid)=> {
          if(valid) {
            // 校验通过
            // 判断新增还是编辑
            if(!this.addForm.id){
              // 调新增接口
            await articlesAdd(this.addForm)
            }else {
              // 掉修改接口
              await articlesUpdate(this.addForm)
            }
            // 提示用户
            this.$message.success(`${this.addForm.id? '更新成功':'新增成功'}`)
            // 关闭弹窗
            this.dialogVisible = false
            // 刷新列表
            this.getTableData()
          }
        })
    },
    async edit(id) {
      
      this.title = '修改技巧'
      this.dialogVisible = true
      // 数据回填
      const res = await articlesDetail(id)
      console.log(res);
      this.addForm = res
    },
    async del(id) {
      this.$confirm('此操作将永久删除该目录, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          // 调删除接口
          await articlesDel(id)
          // 提示用户
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          // 刷新列表
          this.getTableData()
        })
    },
    async changState(id,state) {
      await articlesState(id, state? 0: 1)
      this.getTableData()
    },
    // 分页器
    // 每页条数改变
    handleSizeChange(pagesize) {
      this.form.pagesize = pagesize
      this.getTableData()
    },
    // 当前页改变
    handleCurrentChange(page) {
      this.form.page = page
      this.getTableData()
    },
  }
}
</script>

<style lang="scss" scoped>
.app-container{
  background-color: #fff;
  margin: 10px;
  .top {
    display: flex;
    justify-content: space-between;
  }
}
</style>